package com.mornd.gulimall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mornd.gulimall.common.core.result.AjaxResult;
import com.mornd.gulimall.common.core.utils.SeparatorUtil;
import com.mornd.gulimall.common.core.validation.UpdateGroup;
import com.mornd.gulimall.product.entity.Attr;
import com.mornd.gulimall.product.service.IAttrService;
import com.mornd.gulimall.product.service.ICategoryService;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;

import java.util.List;

import static com.mornd.gulimall.product.enums.AttrType.BASE;
import static com.mornd.gulimall.common.core.result.AjaxResult.error;
import static com.mornd.gulimall.common.core.result.AjaxResult.success;

/**
 * <p>
 * 商品属性 前端控制器
 * </p>
 *
 * @author mornd
 * @since 2023-07-23
 */

@Validated
@RestController
@RequestMapping("/product/attr")
public class AttrController {
    @Resource
    private IAttrService attrService;
    @Resource
    private ICategoryService categoryService;

    @GetMapping
    public AjaxResult list(Attr attr) {
        return success(attrService.list(attr));
    }

    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        Attr attr = attrService.get(id);
        return success(attr);
    }

    @PutMapping
    public AjaxResult save(@RequestBody @Validated Attr attr) {
        if (attr.getAttrType() == BASE.ordinal() && attr.getGroupId() == null) {
            error("所属分组不能为空");
        }
        if (!categoryService.isLeaf(attr.getCatelogId())) {
            return error("必须选择最后一层分类");
        }
        String filterBlank = SeparatorUtil.filterBlank(attr.getValueSelect());
        if(StringUtils.hasText(filterBlank)) {
            attr.setValueSelect(filterBlank);
        } else {
            attr.setValueSelect(null);
        }

        attrService.saveAttr(attr);
        return success();
    }

    @PostMapping
    public AjaxResult update(@RequestBody @Validated(UpdateGroup.class) Attr attr) {
        if (attr.getAttrType() == BASE.ordinal() && attr.getGroupId() == null) {
            error("所属分组不能为空");
        }
        if (!categoryService.isLeaf(attr.getCatelogId())) {
            return error("必须选择最后一层分类");
        }
        attrService.updateAttr(attr);
        return success();
    }

    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable @NotEmpty Long[] ids) {
        attrService.relationRemove(ids);
        return success();
    }

    /**
     * 根据分类id查询销售属性
     *
     * @param catId
     * @return
     */
    @GetMapping("/saleByCatId/{catId}")
    public AjaxResult saleByCatId(@PathVariable Long catId) {
        LambdaQueryWrapper<Attr> qw = Wrappers.<Attr>lambdaQuery().eq(Attr::getCatelogId, catId)
                // [0-销售属性，1-基本属性，2-既是销售属性又是基本属性]
                .in(Attr::getAttrType, 0, 2);
        List<Attr> list = attrService.list(qw);
        return AjaxResult.success(list);
    }

    /**
     * 根据分类id查询 attrGroup 和其对应的attrs 根据分类id查询规格参数(基本属性)
     *
     * @param catId
     * @return
     */
    /**
     * 根据分类id查询 attrGroup 和其对应的 attrs 根据分类id查询规格参数(基本属性)
     * 如果 spuId 有值那么就回显 spu 的值
     * @param catId
     * @param spuId
     * @return
     */
    @GetMapping("/specByCatId")
    public AjaxResult specByCatId(@RequestParam Long catId,
                                  @RequestParam(required = false) String spuId) {
        return AjaxResult.success(attrService.specByCatId(catId, spuId));
    }
}
